package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;

import com.museum.entity.message.ExcavationInformation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class ExcavationServiceImpl extends ServiceImpl<com.museum.mapper.ExcavationInformationMapper, ExcavationInformation> {
    @Autowired
    private FeignAPIController feignAPIController;
    @Autowired
    private com.museum.mapper.ExcavationInformationMapper excMapper;


    /**
     * @param
     * @param request
     * @Desciption 添加考古挖掘信息
     * @return
     */
    public int addExcavationInfo(ExcavationInformation excInfo, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();

        return this.baseMapper.insert(excInfo);

    }

    /**
     * @Description 删除考古挖掘信息
     *
     */
    public int deleteExcInfoById(Integer id){
        return  excMapper.deleteById(id);
    }


    /**
     * @Description 根据 id修改考古信息
     */

    /**
     * @param
     * @Desciption 条件查询征集
     * @return
     */
    public JSONObject getAllExcInfoByPage(long current, long limit, ExcavationInformation excInfo){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<ExcavationInformation> excInfoList = new ArrayList<>();
        Integer id =null;
        String emergeTime =null;
        String leader = null;
        String checkOrganization =null;
        String reportPublish =null;
        String state = null;
        String place = null;
        String organization = null;
        String reportName = null;
        String reportTime = null;
        String note = null;
        if(excInfo!=null){
            id = excInfo.getId();
            emergeTime = excInfo.getEmergeTime();
            leader = excInfo.getLeader();
            checkOrganization = excInfo.getCheckOrganization();
            reportPublish = excInfo.getReport_publish();
            state = excInfo.getState();
            place = excInfo.getPlace();
            organization = excInfo.getOrganization();
            reportName = excInfo.getReportName();
            reportTime = excInfo.getReport_time();
            note = excInfo.getNote();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(emergeTime)) {
            map.put("emergeTime",emergeTime);
        }
        if (!StringUtils.isEmpty(leader)) {
            map.put("leader",leader);
        }
        if (!StringUtils.isEmpty(checkOrganization)) {
            map.put("checkOrganization",checkOrganization);
        }
        if (!StringUtils.isEmpty(reportPublish)) {
            map.put("reportPublish",reportPublish);
        }
        if (!StringUtils.isEmpty(state)) {
            map.put("state",state);
        }
        if (!StringUtils.isEmpty(place)) {
            map.put("place",place);
        }
        if (!StringUtils.isEmpty(organization)) {
            map.put("organization",organization);
        }
        if (!StringUtils.isEmpty(reportName)) {
            map.put("reportName",reportName);
        }
        if (!StringUtils.isEmpty(reportTime)) {
            map.put("reportTime",reportTime);
        }
        if (!StringUtils.isEmpty(note)) {
            map.put("note",note);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<ExcavationInformation> list = excMapper.selectExcavationInfoByPage(map);
        for (ExcavationInformation excavationInformation : list) {
            excInfoList.add(excavationInformation);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",excInfoList.size());
        jsonObject.put("excInfoList",excInfoList);
        return jsonObject;
    }

}
